/*
 * GTranslationHelper.java
 * Created on 21 juin 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.helper;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import com.hockeo.vhbase.data.dao.IGDao;
import com.hockeo.vhbase.data.dao.misc.GLanguage;
import com.hockeo.vhbase.data.dao.misc.GTranslation;
import com.hockeo.vhbase.error.GDataException;

/**
 * Singleton helper class that provides services for retrieving translated text.
 *
 * @version $Id$
 * @author janjanke
 */
public final class GTranslationHelper
{
  private static GTranslationHelper d_instance;
  private ApplicationContext        d_appContext;
  private IGDao                     d_dao;
  private GLanguage                 d_languageDefault;
  private boolean                   d_fSearchedDefaultLang = false;

  private GTranslationHelper()
  {}

  /**
   * Called by Spring after the object is initialised.
   */
  private void init()
  {
    d_instance = this;
  }

  /**
   * Called by Spring when the object is destroyed.
   */
  private void destroy()
  {
    d_instance = null;
  }

  public void setDao( IGDao dao )
  {
    d_dao = dao;
  }

  /**
   * Returns the singleton translation helper instance.
   */
  public static GTranslationHelper get()
  {
    return d_instance;
  }

  public void setApplicationContext( ApplicationContext context ) throws BeansException
  {
    d_appContext = context;
  }

  /**
   * Translates the given text using the specified locale. If a string in the given
   * locale's language is not available, it is tried to retrieve a text in the default
   * language. If this fails as well, the input string is returned as is (i.e. unchanged).
   *
   * @param strToTranslate the text to be translated
   * @param locale the locale to be used for recuperating the target language
   * @return the translated text or the unchanged input string if no matching translation
   *         was found
   */
  public String translate( String strToTranslate, Locale locale )
  {
    GTranslation t = d_dao.findTranslation( strToTranslate, locale.getLanguage() );
    if ( t == null )
    {
      String strDefaultLang = getDefaultLanguage();

      if ( strDefaultLang != null )
        t = d_dao.findTranslation( strToTranslate, strDefaultLang );
    }

    return t != null ? t.getTranslatedText() : strToTranslate;
  }

  private String getDefaultLanguage()
  {
    if ( !d_fSearchedDefaultLang )
    {
      try
      {
        d_languageDefault = d_dao.findDefaultLanguage();
      }
      catch ( GDataException ex )
      {
        return null;
      }
    }

    return d_languageDefault != null ? d_languageDefault.getId() : null;
  }
}
